/*
 * Author: dogguo
 *
 * Time  : 2016/04/30 13:06
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <i386-linux-gnu/sys/io.h>
#include <unistd.h>
#include "../libghttp/ghttp.h"
#include "../libghttp/http_hdrs.h"


#define GET_ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));}

//http://www.elesos.com/index.php?title=Libghttp库使用指南

int net_http_download(char* url, char* file_path) {
    printf("net_http_get <<<--- \n");

    // check path
    if(NULL == opendir(file_path)) {
        printf("net_http_get <<<--- path not exist \n");
        int r = mkdir(file_path, 0777);
        printf("net_http_get <<<--- r = %d \n", r);
        if(r != 0) {
            printf("net_http_get <<<--- path mkdir failed ! \n");
            return 0;
        };
    }

    ghttp_request *request = NULL;
    request = ghttp_request_new();

    if (ghttp_set_uri(request, url) == -1) {
        printf("net_http_get <<<--- ghttp_set_uri failed ! \n");
        return 0;
    }

    if (ghttp_set_type(request, ghttp_type_get) == -1) {
        printf("net_http_get <<<--- ghttp_set_type failed ! \n");
        return 0;
    }

    // prepare request
    ghttp_prepare(request);

    // process request
    ghttp_status status = ghttp_process(request);
    printf("net_http_get <<<--- status code = %d\n", ghttp_status_code(request));
    if (status == ghttp_error) {
        printf("net_http_get <<<--- status error ! \n");
        return 0;
    }

    // get reponse content body
    char* recv_buf = ghttp_get_body(request);

    // get reponse content length
    int recv_len = ghttp_get_body_len(request);
    printf("net_http_get <<<--- recv_len = %d \n", recv_len);

    char *file_name = basename(url);
    if(file_name == NULL) {
        printf("net_http_get <<<--- basename is NULL ! \n");
        return 0;
    }
    printf("net_http_get <<<--- file_name = %s \n", file_name);

    int file_path_len = strlen(file_path);
    int file_name_len = strlen(file_name);
    char file_full_path[file_path_len + file_name_len];
    arr_char_pcpy(file_full_path, 0, file_path, file_path_len);
    arr_char_pcpy(file_full_path, file_path_len, file_name, file_name_len + 1);
    printf("net_http_get <<<--- file_full_path = %s \n", file_full_path);

    // file write
    FILE* file = fopen(file_full_path, "wb");
    if(file == NULL) {
        printf("net_http_get <<<--- fopen error ! \n");
        return 0;
    }

    if(fwrite(recv_buf, 1, recv_len, file) != recv_len) {
        printf("net_http_get <<<--- fwrite failed ! \n");
        return 0;
    }
    fclose(file);

    printf("net_http_get --->>> \n");
    return 1;
}


/*
 *http get method
 */
int net_http_get(char* url, char* param, int timeout, char* result_buf) {
    printf("net_http_get <<<--- \n");

    int result_len = -1;

    ghttp_request *request = NULL;

    // init new request
    request = ghttp_request_new();

    // parse url
    if(param != NULL && strlen(param) > 0) {
        char tmp[1024];
        strcpy(tmp, url);
        if(strchr(tmp, '?') == NULL) {//url not exsit
            strcat(tmp,"?")  ;
        }
        strcat(tmp, param) ;
        ghttp_set_uri(request, tmp);
    } else {
         ghttp_set_uri(request, url);
    }

    // set request method type
    ghttp_set_type(request, ghttp_type_get);

    // set header content
    ghttp_set_header(request, http_hdr_Connection, "close");
    char timeout_str[10];
    ghttp_set_header(request, http_hdr_Timeout, timeout_str);

    // prepare request
    ghttp_prepare(request);

    // process request
    ghttp_process(request);

    // get reponse content body
    char* tmp_buf = ghttp_get_body(request);
    strcpy(result_buf, tmp_buf);// copy data
    printf("net_http_get <<<--- result_buf =  %s \n", result_buf);

    // get reponse content length
    result_len = ghttp_get_body_len(request);

    // destroy request src
    ghttp_request_destroy(request);

    printf("net_http_get --->>> \n");
    return result_len;
}

int net_http_post(char* url, char* param, int timeout, char* result_buf) {
    printf("net_http_post <<<--- \n");

    int result_len = -1;

    char szVal[1024];
    ghttp_request *request = NULL;
    ghttp_status status;
    int len;
    printf("%s\n", param); //test
    request = ghttp_request_new();
    if (ghttp_set_uri(request, url) == -1) {
        return -1;
    }

    if (ghttp_set_type(request, ghttp_type_post) == -1) {  //post
        return -1;
    }

    ghttp_set_header(request, http_hdr_Content_Type,"application/x-www-form-urlencoded");

    char timeout_str[10];
    ghttp_set_header(request, http_hdr_Timeout, timeout_str);

    //ghttp_set_sync(request, ghttp_sync); //set sync
    len = strlen(param);
    ghttp_set_body(request, param, len);

    // prepare request
    ghttp_prepare(request);

    // process request
    status = ghttp_process(request);
    if (status == ghttp_error) {
        return -1;
    }

    // get reponse content body
    char* tmp_buf = ghttp_get_body(request);
    strcpy(result_buf, tmp_buf);// copy data
    printf("net_http_post <<<--- result_buf =  %s \n", result_buf);

    // get reponse content length
    result_len=ghttp_get_body_len(request);

    ghttp_clean(request);

    printf("net_http_post --->>> \n");
    return result_len;
}
